/*
 * Name: Joon Ho Cho
 * Date: 10/24/2008
 * Course: 15-437
 */
package scheduler.model;


import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.dao.GenericDAO;

import scheduler.databean.Section;
import scheduler.formbean.searchForm;

public class SectionDaoBeanFactoryImpl
    extends GenericDAO<Section> 
    implements SectionDao { 
	
    public SectionDaoBeanFactoryImpl(String dbDriver, String dbUrl) {
        this(BeanTable.getSQLInstance(Section.class, "section", dbDriver, dbUrl));
    }
    
    private SectionDaoBeanFactoryImpl(BeanTable<Section> table) {
        super(table, "id");
        getTable().setIdleConnectionCleanup(true);
    }
    
    public Section create(String[] classroom, int courseId, String[] Day, int[] duration, String Ins, int lect, String Sec, String Sem, String[] time, int yr)
        throws DAOException {    	
            Section newSection = new Section(courseId + Sec);
            newSection.setClassroom(classroom);
            newSection.setCourseId(courseId);
            newSection.setDay(Day);
            newSection.setDuration(duration);
            newSection.setInstructor(Ins);
            newSection.setLectureId(lect);
            newSection.setSectionName(Sec);
            newSection.setSemester(Sem);
            newSection.setStartTime(time);
            newSection.setYear(yr);
            newSection = create(newSection);
           
            return newSection;
    }
    
    public String parseTime(String time) {
		String timeParsed = time.substring(0, 2) + time.substring(3, 5);
		if (time.charAt(5) == 'P' && ! time.substring(0, 2).equals("12")) {
			timeParsed = (Integer.parseInt(time.substring(0, 2)) + 12) + time.substring(3, 5);
		}
		return timeParsed;
    }
    
    public int calDuration(String startTime, String endTime) {
		int duration = (Integer.parseInt(endTime.substring(0, 2)) - Integer.parseInt(startTime.substring(0, 2))) * 60;
		duration += Integer.parseInt(endTime.substring(2)) - Integer.parseInt(startTime.substring(2));
		duration = (duration / 30 + ((duration % 30 != 0)? 1 : 0)) * 30;
    	return duration;
    }
    
    public int countLine(String info) {
    	int count = 1;
    	for (int i = 0; i < info.length(); i++)
    		if (info.charAt(i) == '/')
    			count++;

    	return count;
    }
    
    public Section create(int courseId, int lectureId, String semester, int year, String info) throws DAOException {
		int numLine = countLine(info);
		String[] dayArray = new String[numLine];
		String[] startTimeArray = new String[numLine];
		int[] durationArray = new int[numLine];
		String[] classroomArray = new String[numLine];
    	
		int sectionName = info.indexOf(' ');
		String sectionNameString = info.substring(0, sectionName);
		
        Section newSection = new Section(courseId + sectionNameString);
        newSection.setCourseId(courseId);
        newSection.setLectureId(lectureId);
        newSection.setSectionName(sectionNameString);
        newSection.setYear(year);
        newSection.setSemester(semester);
        
        int nextLine = sectionName + 1;
		for (int i = 0; i < numLine; i++) {
			//A M 12:30PM 01:20PM CFA 206A Instructor TBA / M 06:30PM 09:20PM HH 1305

			int day = info.indexOf(' ', nextLine);
			int startTime = info.indexOf(' ', day + 1);
			int endTime = info.indexOf(' ', startTime + 1);
			int building = info.indexOf(' ', endTime + 1);
			int room = info.indexOf(' ', building + 1);

			dayArray[i] = info.substring(nextLine, day);

			startTimeArray[i] = parseTime(info.substring(day + 1, startTime));
			durationArray[i] = calDuration(startTimeArray[i], parseTime(info.substring(startTime + 1, endTime)));
			classroomArray[i] = info.substring(endTime + 1, building) + " " + ((room == -1)? info.substring(building + 1) : info.substring(building + 1, room));
			
			nextLine = info.indexOf('/', nextLine) + 2;
			if (i == 0) {
				newSection.setInstructor( (numLine == 1)? info.substring(room + 1) : info.substring(room + 1, nextLine - 3));
			}
		}
		
        newSection.setDay(dayArray);
        newSection.setStartTime(startTimeArray);
        newSection.setDuration(durationArray);
        newSection.setClassroom(classroomArray);
        newSection = create(newSection);
       
        return newSection;
    }
    
    public Section[] getSectionForCourse(int courseId) throws DAOException {
        try {
            return getFactory().match(MatchArg.equals("CourseId", courseId));
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }

    public Section[] getSectionForSemester(int courseID, String Semester, int year) throws DAOException {
        try {
            return getFactory().match(MatchArg.equals("courseId", courseID),MatchArg.equals("semester", Semester), MatchArg.equals("year", year) );
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }
       
    public Section[] getSearch(searchForm form) throws DAOException {
        try {
        	
        	MatchArg Semester= 		MatchArg.greaterThan("year",0);
        	MatchArg Year=			MatchArg.greaterThan("year",0);
        	MatchArg Classroom= 	MatchArg.greaterThan("year",0);
        	MatchArg Instructor= 	MatchArg.greaterThan("year",0);
        	MatchArg Day= 			MatchArg.greaterThan("year",0);
        	
        	System.out.println(form.getInstructor());
        	if(form.getSemester()!=null && form.getSemester().length()!=0){
        		Semester= MatchArg.containsIgnoreCase("semester", form.getSemester());        		
        	}
        	
        	if(form.getYear()>=1){
        		Year= MatchArg.equals("year", form.getYear());  
        		System.out.println("ddddddtd");
        	}     	

        	if(form.getClassroom()!=null && form.getClassroom().length()!=0){
        		System.out.println("ddddddd");
        		Classroom= MatchArg.containsIgnoreCase("classroom", form.getClassroom());   
        	}
        	
        	if(form.getInstructor()!=null && form.getInstructor().length()!=0){
        		System.out.println("ddddddd");
        		Instructor= MatchArg.containsIgnoreCase("instructor", form.getInstructor());   
        	}
        	
        	if(form.getDay()!=null && form.getDay().length()!=0){
        		System.out.println("ddddddd");
        		Day= MatchArg.containsIgnoreCase("day", form.getDay());   
        	}  	
        	System.out.println("match"+getFactory().match(Semester,Year,Classroom,Instructor, Day).length);
        	return getFactory().match(Semester,Year,Classroom,Instructor, Day);
            
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }
    
    public Section lookup(String id) throws DAOException {
        try {
            return getFactory().lookup(id);
        } catch (RollbackException e) {
            throw new DAOException(e);
        }
    }

	
    


	
}
